package com.app.tablet.view;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.app.tablet.R;
import com.app.tablet.data.dao.TabletDao;
import com.app.tablet.data.tasks.MainPageTask;
import com.app.tablet.notify.Notifier;

public class MedNameListActivity extends BaseActivity implements Notifier,
		OnClickListener, OnItemClickListener {
	
	private static final String TagProject = "Tablet-Project";
	private static final String TagPage = "MedNameListActivity-Page ::";

	TextView titletxt;
	Button leftBtn;
	Button rightBtn;
	ListView list_Tabletname;
	mycursoradaptor mycursoradap;
	Cursor myCursor = null;
	private String mednameSelected = null;
	boolean[] isselected;
	private int selectedPosition = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_mednamelistpage);
		setHeaderContent();

		list_Tabletname = (ListView) findViewById(R.id.list_medname);
		list_Tabletname.setOnItemClickListener(this);
		leftBtn.setOnClickListener(this);
		rightBtn.setOnClickListener(this);

		startTask();

	}

	private void setHeaderContent() {
		leftBtn = (Button) findViewById(R.id.leftHeaderBtn);
		leftBtn.setText(R.string.cancelBtn);

		rightBtn = (Button) findViewById(R.id.rightHeaderBtn);
		rightBtn.setText(R.string.doneBtn);

		titletxt = (TextView) findViewById(R.id.titleHeaderTxt);
		titletxt.setText(R.string.titletMainpageTxt);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftHeaderBtn:
			this.finish();
			break;
		case R.id.rightHeaderBtn:
			if (selectedPosition != -1) {
				AddPageActivity.dbData.setTabletname(mednameSelected);
				this.finish();
			}else{
				Toast.makeText(getApplicationContext(), "Choose a tablet.", 500).show();
			}
			break;
		}
	}

	@Override
	public void onSuccess(Object response) {
		hideProgress();
		if (response instanceof Cursor) {
			populateMainList((Cursor) response);
		}
	}

	@Override
	public Object onError() {
		// TODO Auto-generated method stub
		return null;
	}

	private void startTask() {
		showDialog(DIALOG_ID_PROGRESS);
		MainPageTask mainTask = new MainPageTask(this);
		mainTask.execute();
	}

	private void populateMainList(Cursor mainpageCursor) {
		myCursor = mainpageCursor;
		mycursoradap = new mycursoradaptor(this, myCursor);
		isselected = new boolean[mainpageCursor.getCount()];
		list_Tabletname.setAdapter(mycursoradap);
	}

	class mycursoradaptor extends CursorAdapter {

		Context adcontext;

		public mycursoradaptor(Context context, Cursor c) {
			super(context, c);
			adcontext = context;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView txtname = (TextView) view.findViewById(R.id.txt_medicinename);
			ImageView imgvw1 = (ImageView) view.findViewById(R.id.img_tick_medname);
			txtname.setText(cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_TABLET_NAME)));
			if (isselected[cursor.getPosition()]) {
				imgvw1.setVisibility(View.VISIBLE);
				mednameSelected = cursor.getString(cursor.getColumnIndex(TabletDao.FIELD_TABLET_NAME));
			} else {
				imgvw1.setVisibility(View.INVISIBLE);
			}
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) adcontext.getSystemService(adcontext.LAYOUT_INFLATER_SERVICE);
			View rowview = inflater.inflate(R.layout.layout_listitem_mednamelist, null);
			return rowview;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
		for (int index = 0; index < isselected.length; index++) {
			isselected[index] = false;
		}
		isselected[position] = true;
		selectedPosition = position;
		refreshListView();
	}

	private void refreshListView() {
		list_Tabletname.invalidate();
		mycursoradap.notifyDataSetChanged();
	}

}